import { ThunkAction } from 'redux-thunk'
import { RootState } from '../store'
import axios from 'axios'

// 正在调用信息api
export const FETCH_RECOMMEND_PRODUCTS_START = 'FETCH_RECOMMEND_PRODUCTS_START'
// 正在调用信息api成功
export const FETCH_RECOMMEND_PRODUCTS_SUCCESS =
	'FETCH_RECOMMEND_PRODUCTS_SUCCESS'
// 正在调用信息api失败
export const FETCH_RECOMMEND_PRODUCTS_FAIL = 'FETCH_RECOMMEND_PRODUCTS_FAIL'

interface FetchRecommendProductStartAction {
	type: typeof FETCH_RECOMMEND_PRODUCTS_START
}
interface FetchRecommendProductSuccessAction {
	type: typeof FETCH_RECOMMEND_PRODUCTS_SUCCESS
	payload: any
}
interface FetchRecommendProductFailAction {
	type: typeof FETCH_RECOMMEND_PRODUCTS_FAIL
	payload: any
}

export type RecommendProductAction =
	| FetchRecommendProductStartAction
	| FetchRecommendProductSuccessAction
	| FetchRecommendProductFailAction

export const fetchRecommendProductStartActionCreator =
	(): FetchRecommendProductStartAction => {
		return {
			type: FETCH_RECOMMEND_PRODUCTS_START,
		}
	}

export const fetchRecommendProductSuccessActionCreator = (
	data
): FetchRecommendProductSuccessAction => {
	return {
		type: FETCH_RECOMMEND_PRODUCTS_SUCCESS,
		payload: data,
	}
}

export const fetchRecommendProductFailActionCreator = (
	error
): FetchRecommendProductFailAction => {
	return {
		type: FETCH_RECOMMEND_PRODUCTS_FAIL,
		payload: error,
	}
}

export const giveMeDataActionCreator =
	(): ThunkAction<void, RootState, unknown, RecommendProductAction> =>
	async (dispatch, getState) => {
		dispatch(fetchRecommendProductStartActionCreator())
		try {
			// 接口不通，没有 x-icode ， 列表还是使用 假数据
			const { data } = await axios.get(
				`http://123.56.149.216:8080/api/productCollections`
			)
			dispatch(fetchRecommendProductSuccessActionCreator(data))
		} catch (error: any) {
			dispatch(fetchRecommendProductFailActionCreator(error.message))
		}
	}
